<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-people"></i> 用户管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>用户列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row>
                    <el-col :span="2">
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            class="handle-del mr10"
                            @click="delAllSelection"
                        >批量删除</el-button>
                    </el-col>
                    <el-col :span="2" v-if="is_show_switch && is_disabled">
                        <el-button
                            type="primary"
                            icon="el-icon-edit"
                            class="handle-del mr10"
                        >批量中奖用户</el-button>
                    </el-col>
                    <!--<el-col :span="2" v-else>
                        <el-button
                                type="primary"
                                icon="el-icon-edit"
                                class="handle-del mr10"
                        >批量中奖用户</el-button>
                    </el-col>-->
                    <el-col :span="16" style="display:flex;justify-content: center;">
                        <el-select
                            v-model="query.sort"
                            placeholder="选择排序方式"
                            style="width:120px;margin-right:10px"
                        >
                            <el-option key="1" label="最新" value="1"></el-option>
                            <el-option key="2" label="正常" value="2"></el-option>
                        </el-select>
                        <el-select
                            v-model="query.status"
                            placeholder="状态"
                            class="handle-select mr10"
                        >
                            <el-option label="全部状态" value></el-option>
                            <el-option key="1" label="正常" value="1"></el-option>
                            <el-option key="2" label="禁用" value="2"></el-option>
                            <el-option key="3" v-if="is_show_switch  && is_disabled" label="中奖用户" value="3"></el-option>
                            <el-option key="4" v-if="is_show_switch  && is_disabled" label="不中奖用户" value="4"></el-option>
                        </el-select>
                        <!--<el-select-->
                            <!--v-model="query.type"-->
                            <!--placeholder="用户身份"-->
                            <!--class="handle-select mr10"-->
                        <!--&gt;-->
                            <!--<el-option label="不限" value="0"></el-option>-->
                            <!--<el-option key="1" label="注册用户" value="1"></el-option>-->
                            <!--<el-option key="2" label="经销商" value="2"></el-option>-->
                            <!--<el-option key="3" label="店主" value="3"></el-option>-->
                            <!--<el-option key="4" label="经销商&店主" value="4"></el-option>-->
                        <!--</el-select>-->
                        <el-input
                            v-model="query.name"
                            placeholder="用户昵称|ID"
                            class="handle-input mr10"
                            style="width:120px;"
                        ></el-input>
                        <el-input
                            style="width:130px;"
                            v-model="query.mobile"
                            placeholder="用户手机号"
                            class="handle-input mr10"
                        ></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                        <el-button type="primary" icon="el-icon-download" @click="exportSource">导出数据</el-button>
                    </el-col>
                </el-row>
            </div>
            <el-table
                :data="tableData"
                border
                v-loading="loading"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                id="usertable"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center">
                   <template
                        slot-scope="scope"
                    >
                    <el-tooltip v-if="scope.row.is_yx == 1" class="item" effect="dark" content="有效用户" placement="bottom">
                       <span  style="color:red">{{scope.row.id}}</span>
                     
                    </el-tooltip>
                   <el-tooltip v-else class="item" effect="dark" content="无效用户" placement="bottom">
                      
                       <span  >{{scope.row.id}}</span>
                    </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column label="昵称" align="center">
                    <template
                        slot-scope="scope"
                    >{{scope.row.name&&scope.row.name.length>0?scope.row.name:'无'}}</template>
                </el-table-column>
                <el-table-column prop="mobile" label="手机号码"></el-table-column>
                <!-- <el-table-column label="头像" align="center">
                    <template slot-scope="scope">
                        <el-image
                            v-if="scope.row.avatar"
                            class="table-td-thumb"
                            :src="api.root_url+scope.row.avatar"
                            :preview-src-list="[api.root_url+scope.row.avatar]"
                        ></el-image>
                        <span v-else>无</span>
                    </template>
                </el-table-column>-->
                <el-table-column  label="余额">
                        <template slot-scope="scope">
                            <el-tag
                            >{{scope.row.money}} &nbsp; <span style='color:red' class="el-icon-circle-plus-outline" @click="editUserMoney(scope.row,'money')"></span></el-tag>
                        </template>
                </el-table-column>
            <!--    <el-table-column  label="BUC">
                         <template slot-scope="scope">
                            <el-tag
                            >{{scope.row.buc}} &nbsp; <span style='color:red' class="el-icon-circle-plus-outline" @click="editUserMoney(scope.row,'buc')"></span></el-tag>
                        </template>
                </el-table-column>-->
                <!--<el-table-column prop="coin" label="积分数量">-->
                    <!--<template slot-scope="scope">-->
                        <!--<el-tag-->
                        <!--&gt;{{scope.row.coin}} &nbsp; <span style='color:red' class="el-icon-circle-plus-outline" @click="editUserMoney(scope.row,'coin')"></span></el-tag>-->
                    <!--</template>-->
                <!--</el-table-column>-->
                <!--<el-table-column prop="coin" label="锁定积分">-->
                    <!--<template slot-scope="scope">-->
                        <!--<el-tag-->
                        <!--&gt;{{scope.row.lockcoin}} &nbsp; <span style='color:red' class="el-icon-circle-plus-outline" ></span></el-tag>-->
                    <!--</template>-->
                <!--</el-table-column>-->
                <!--<el-table-column prop="coin" label="佣金">-->
                    <!--<template slot-scope="scope">-->
                        <!--<el-tag-->
                        <!--&gt;{{scope.row.commission}} &nbsp; <span style='color:red' class="el-icon-circle-plus-outline" ></span></el-tag>-->
                    <!--</template>-->
                <!--</el-table-column>-->
                <!--<el-table-column  label="用户名">-->
                    <!--<template slot-scope="scope" >-->
                       <!--<span v-if="scope.row.take_name!=''">-->
                           <!--{{scope.row.take_name}}-->
                      <!--</span>-->
                      <!--<span v-else>未知</span>-->
                <!--</template>-->
                <!--</el-table-column>-->
                <!--<el-table-column prop="p_name" label="邀请人"></el-table-column>-->
                <!--<el-table-column prop="invite_count" label="直推人数"></el-table-column>-->
                <!--<el-table-column prop="invite_code" label="邀请码"></el-table-column>-->
                <!--<el-table-column label="邀请二维码" align="center">-->
                    <!--<template slot-scope="scope">-->
                        <!--<el-image-->
                            <!--v-if="scope.row.invite_qrcode"-->
                            <!--class="table-td-thumb"-->
                            <!--:src="api.root_url+scope.row.invite_qrcode"-->
                            <!--:preview-src-list="[api.root_url+scope.row.invite_qrcode]"-->
                        <!--&gt;</el-image>-->
                        <!--<span v-else>无</span>-->
                    <!--</template>-->
                <!--</el-table-column>-->

                <el-table-column label="状态" sortable align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.status==1?'success':'danger'"
                        >{{scope.row.status==1?'正常':'禁用'}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="设置为打手" sortable align="center">
                    <template slot-scope="scope">
                        <el-tag
                                :type="scope.row.ds_status==1?'danger':'success'"
                        >{{scope.row.ds_status==1?'未设置':'已设置'}}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column v-if="is_show_switch  && is_disabled" label="中奖用户" align="center">
                <template slot-scope="scope">
                    <el-switch
                      v-model="scope.row.is_zj_open"
                      @change="switchOpen(scope.row, 'is_zj_open')"
                      active-color="#13ce66"
                      inactive-color="#dcdfe6">
                    </el-switch>
                </template>
                </el-table-column>
               <!-- <el-table-column v-else label="中奖用户" align="center">
                    <template slot-scope="scope">
                        <el-switch
                                active-color="#13ce66"
                                inactive-color="#dcdfe6">
                        </el-switch>
                    </template>
                </el-table-column>-->

                <el-table-column v-if="is_show_switch  && is_disabled" label="不中奖用户" align="center">
                    <template slot-scope="scope">
                        <el-switch
                                v-model="scope.row.is_bzj_open"
                                @change="switchOpen(scope.row, 'is_bzj_open')"
                                active-color="#13ce66"
                                inactive-color="#dcdfe6">
                        </el-switch>
                    </template>
                </el-table-column>
               <!-- <el-table-column v-else label="不中奖用户" align="center">
                    <template slot-scope="scope">
                        <el-switch
                                active-color="#13ce66"
                                inactive-color="#dcdfe6">
                        </el-switch>
                    </template>
                </el-table-column>-->

                <el-table-column prop="updated_at" label="更新时间"></el-table-column>
                <el-table-column prop="created_at" label="注册时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            @click="updateStatus(scope.row.id,scope.$index,scope.row.status)"
                            :icon="scope.row.status==1?'el-icon-lx-attentionforbid':'el-icon-lx-attention'"
                            type="text"
                            :style="{color:scope.row.status==2?'#67C23A':'#E6A23C'}"
                        >{{ scope.row.status==1?'禁用':'解禁' }}</el-button>
                        <el-button
                                @click="updateDSStatus(scope.row.id,scope.$index,scope.row.ds_status)"
                                :icon="scope.row.ds_status==1?'el-icon-lx-attentionforbid':'el-icon-lx-attention'"
                                type="text"
                                :style="{color:scope.row.ds_status==2?'#67C23A':'#E6A23C'}"
                        >{{ scope.row.ds_status==1?'设置为打手':'取消为打手' }}</el-button>
                        <!--<el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑积分</el-button>-->
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                        <!--<el-button-->
                            <!--type="text"-->
                            <!--icon="el-icon-set-up"-->
                            <!--style="color:#67C23A"-->
                            <!--@click="handleRation(scope.$index, scope.row)"-->
                        <!--&gt;绑定关系</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                     layout="total, prev, pager, next,sizes"
                    :page-sizes="[0,10, 20, 50,100 ,500,1000]"
                    @size-change="handleSizeChange"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <el-dialog title="编辑积分" v-dialogDrag :visible.sync="ticketVisible" width="580px">
            <el-form ref="edit" :model="ticket" label-width="260px">
                <el-form-item label="积分数量" class="form-item">
                    <el-input
                        v-model="ticket.ticket"
                        maxlength="30"
                        type="number"
                        minlength="2"
                        placeholder="请输入积分数量"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="ticketVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveTicket">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog :title="editMoneytitle" v-dialogDrag :visible.sync="editMoneyVisible" width="580px">
            <el-form ref="edit" :model="userMoneyForm" label-width="120px">
                <el-form-item label="操作类型" class="form-item">
                    <el-select v-model="userMoneyForm.type" placeholder="请选择操作类型">
                      <el-option label="+" value="+"></el-option>
                      <el-option label="-" value="-"></el-option>
                    </el-select>
                   
                </el-form-item>
                <el-form-item label="操作数量" class="form-item">
                   <el-input
                        v-model="userMoneyForm.num"
                        maxlength="30"
                        type="number"
                        minlength="2"
                        placeholder="请输入数量"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editMoneyVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveUserMoney" :loading="editMoneyLoading">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="绑定关系" v-dialogDrag :visible.sync="rationVisible" width="800px" height="600px">
            <div class="ration-box" >
              <el-card class="box-card">
                <div slot="header" class="clearfix">
                  <span>我的信息</span>
                  <!-- <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button> -->
                </div>
                <div class="" style='display:flex;justify-content:center;'>
                  <div style=" width:30%; height:100%;display:flex;justify-content: center; align-items: center;">
                    <el-image
                      style="height:80px;width:80px;border-radio:50%;"
                      :src="myinfo.avatar"
                    ></el-image>
                  </div>
                  <div style="width:70%;height:100%;">
                      <div class="text item">
                        id:{{myinfo.id}}
                      </div>
                      <div class="text item">
                        手机号：{{myinfo.mobile}}
                      </div>
                  </div>
                </div>
              </el-card>
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>我的上级</span>
                <el-button style="float: right; padding: 3px 0" type="text" @click="editParent">修改</el-button>
              </div>
               <div class="" style='display:flex;justify-content:center;' v-if="showedit">
                <div style=" width:30%; height:100%;display:flex;justify-content: center; align-items: center;">
                        <el-image
                          style="height:80px;width:80px;border-radio:50%;"
                          :src="pinfo.avatar"
                        ></el-image>
                      </div>
                  <div style="width:70%;height:100%;">
                      <div class="text item">
                        id:{{pinfo.id}}
                      </div>
                      <div class="text item">
                        手机号：{{pinfo.mobile}}
                      </div>
                  </div>
               </div>
               <div v-else>
                  <el-form ref="form" :model="editFormParent" label-width="80px">
                      <el-form-item label="上级ID">
                        <el-input v-model="editFormParent.pid" placeholder="请输入上级ID"></el-input>
                      </el-form-item>
                    </el-form>
               </div>
            </el-card>
            </div>  
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelParent">取 消</el-button>
                <el-button type="primary" @click="saveRation">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
// import func from 'vue-editor-bridge';

export default {
  name: 'basetable',
  data () {
    return {
      query: {
        type: '',
        name: '',
        mobile: '',
        page: 1,
        pageSize: 10,
        status: '',
        sort: '1'
      },
      valueaaaa: true,
      editMoneyLoading:false,
      userMoneyForm:{type:'',num:0,id:''},
      editMoneytitle:'修改用户余额',
      editMoneyVisible:false,
      editFormParent:{pid:''},
      showedit:true,
      rationVisible:false,
      myinfo:{},
      pinfo:{},
      ticket: { ticket: '' },
      ticketVisible: false,
      uid: 0,
      loading: false,
      tableData: [],
      is_show_switch: false,
      is_disabled: false,
      multipleSelection: [],
      pageTotal: 0,
      delList: [],
      idx: -1,
      id: -1,
      //获取列表请求
      getList: function () {
        let that = this;
        this.api.get('user', this.query, res => {
          that.tableData = res.data.list;
          that.is_show_switch = res.data.is_show_switch;
          that.is_disabled = res.data.is_disabled;
          that.query.page = res.data.page;
          that.query.pageSize = res.data.pageSize;
          that.pageTotal = res.data.pageTotal;
        }, undefined, undefined, this);
      },
      //删除请求
      delLists: function (ids, msg) {
        let that = this;
        if (ids.length < 1) {
          this.$message.error('请选择删除的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.delete('delUsers', { ids: ids }, res => {
            that.query.page = 1;
            that.getList();
            that.multipleSelection = [];
            that.$message.success(msg);
          }, undefined, undefined, this);
        }).catch(() => { });
      }
    };
  },
  created () {
    if (this.$route.query && this.$route.query.user_id_u && this.$route.query.user_id_u != this.query.name) {
      this.query.name = this.$route.query.user_id_u;
    }
    this.getList();
  },

  watch: {
    $route () {
      if (this.$route.query && this.$route.query.user_id_u && this.$route.query.user_id_u != this.query.name) {
        this.query.name = this.$route.query.user_id_u;
        this.query.page = 1;
        this.getList();
      }
    }
  },
  methods: {
     exportSource(){
      this.api.exportExcel(document.querySelector('#usertable'),'用户记录数据表')
    },
    handleSizeChange(val){
      console.log(val)
      if(val == 0){
        this.query.pageSize = 100000000;
      }else{
        this.query.pageSize = val;
      }
      this.getList();
    },
    cancelParent(){
      this.showedit = true;
      this.rationVisible = false;
      this.myinfo={};
      this.editFormParent={pid:''}
    },
    editParent(){
      this.showedit = false;
    },
    editUserMoney(item,type){
       
       this.editMoneytitle =type == 'money'?'修改用户余额':(type == 'coin'?'修改用户积分':(type == 'buc'?'修改用户buc':'修改用户账户'));
        this.editMoneytitle+='【当前：'+(type == 'money'?item.money:(type=='buc'?item.buc:(type=='coin'?item.coin:0)))+'】';
       this.editMoneyVisible=true;
       this.userMoneyForm={type:'',num:'',id:item.id,class:type};

    },
    saveUserMoney(){
        let _this =this;
        if(this.userMoneyForm.id == ''){
            this.$message.error('请选择要修改的用户');
            return false;
        }
        if(this.userMoneyForm.type == ''){
           this.$message.error('请选择要操作用户的类型');
            return false;
        }
        if(this.userMoneyForm.num <= 0){
           this.$message.error('请选择要操作用户的数量');
            return false;
        }
        if(this.editMoneyLoading == false){
            _this.editMoneyLoading=true
            this.api.post('edituserdata',_this.userMoneyForm,res=>{
                if(res.code == 1000){
                    _this.$message.success('修改成功');
                    _this.getList();
                    _this.editMoneyLoading=false;
                    _this.editMoneyVisible = false;
                }

            },err=>{
               _this.editMoneyLoading=false;
            })
        }

    },
    saveRation(){
        let _this=this;
        if(this.editFormParent.pid == ''){
          this.$message.error('请输入要修改的上级ID');
          return false;
        }
        this.$confirm('确定修改上级关系吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.api.post('editparent',{uid:_this.myinfo.id,pid:_this.editFormParent.pid},res=>{
             if(res.code  == 1000){
                this.$message.success('绑定成功');
                _this.showedit = true;
                _this.rationVisible = false;
                _this.myinfo={};
                _this.editFormParent={pid:''}
                _this.getList()
             }
          })
        })
    },
    handleRation(index,row){
        this.rationVisible = true
        this.myinfo = row;
        let _this=this;
        this.api.post('pinfo',{pid:row.pid},res=>{
          _this.pinfo = res.data
        })
    },
    // 触发搜索按钮
    handleSearch () {
      this.$set(this.query, 'page', 1);
      this.getList();
    },


    /**
     * 编辑积分事件
     */
    handleEdit (index, row) {
      this.ticket = { ticket: row.coin, id: row.id };
      this.ticketVisible = true;
    },

    /***
     * 保存积分事件
     */
    saveTicket () {
      this.api.put('user-ticket', this.ticket, res => {
        this.ticketVisible = false;
        this.$message.success('操作成功');
        this.getList();
      }, undefined, undefined, this);
    },

    /**
     * 查看商店
     */
    showShop (user_id) {
      this.$router.push({
        name: `shop`,
        query: {
          user_id_s: user_id
        }
      });
    },

    /**
     * 操作用户状态
     */
    updateStatus (id, index, status) {
      this.$confirm('确定要' + (status == 1 ? '禁用' : '解禁') + '该用户吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.api.put('user-status', { id: id }, res => {
          this.tableData[index].status = status == 1 ? 2 : 1;
          this.$message.success(res.msg);
        });
      }).catch(() => { });

    },
      updateDSStatus (id, index, status) {
      this.$confirm('确定要' + (status == 1 ? '设置' : '关闭') + '该用户为打手吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.api.put('user_ds_status', { id: id }, res => {
          this.tableData[index].ds_status = status == 1 ? 2 : 1;
          this.$message.success(res.msg);
        });
      }).catch(() => { });

    },

    /**
     * 设置开关
     */
    switchOpen(row, field) {
        if (row.is_zj_open && row.is_bzj_open) {
            row[field] = false;
            this.$message.error('请先关闭另一个开关！');
            return false;
        }
        row['zj_field'] = field;
        this.$confirm('确定要操作吗？', '提示', {
          type: 'warning'
        }).then(() => {
            this.api.post('editswitch',{row: row},res=>{
                 this.$message.success(res.msg);
                 //this.getList();
            })
        }).catch(() => { row[field] = row[field] ? false : true; });
    },

    /**
     * 设置取消经销商
     */
    handleDealer (index, id, type) {
      this.$confirm('确定要' + (type == 1 || type == 3 ? '撤销' : '设为') + '经销商吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.api.put('user-dealer', { id: id }, res => {
          this.tableData[index].type = res.data.type;
          this.$message.success(res.msg);
        });
      }).catch(() => { });
    },

    // 多选操作
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },

    // 删除操作
    handleDelete (index, row) {
      this.delLists([row.id], '删除成功');
    },
    //批量删除
    delAllSelection () {
      const length = this.multipleSelection.length;
      let str = '';
      this.delList = this.delList.concat(this.multipleSelection);
      let ids = [];
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' ';
        ids.push(this.multipleSelection[i].id);
      }
      this.delLists(ids, `删除了${str}`);
    },
    //中奖批量选中 暂无用
    editAllSelection () {
      const length = this.multipleSelection.length;
      this.delList = this.delList.concat(this.multipleSelection);
      let uIds = [];
      for (let i = 0; i < length; i++) {
        uIds.push(this.multipleSelection[i].id);
      }
      this.switchOpen(uIds);
    },

    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    }
  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.box-card{
  width:50%;
  height:180px;
}
.ration-box{
  display: flex;
  justify-content: center;
  align-items: center;
}
.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
